package com.mf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mf.entity.bo.UserBo;
import com.mf.entity.req.UserReq;
import com.mf.entity.req.search.UserSearchReq;
import com.mf.groups.AddGroupsInterface;
import com.mf.groups.UpdateGroupsInterface;
import com.mf.result.Result;
import com.mf.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author mf
 * @since 2025-03-08
 */
@RestController
@RequestMapping("user/v1")
@Tag(name = "用户表")
@Slf4j
public class UserController {
    @Resource
    private IUserService userService;

    @Operation(summary = "新增用户")
    @PostMapping("save")
    public Result<Boolean> save(@Validated(AddGroupsInterface.class) @RequestBody UserReq userReq) {
        log.info("新增用户:{}", userReq);
        return Result.success(userService.save(getUserBo(userReq)));
    }

    @Operation(summary = "更新用户")
    @PostMapping("update")
    public Result<Boolean> update(@Validated(UpdateGroupsInterface.class) @RequestBody  UserReq userReq) {
        userReq.setUName(null);
        return Result.success(userService.updateById(getUserBo(userReq)));
    }

    @Operation(summary = "删除用户")
    @GetMapping("delete/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") List<Integer> ids) {
        return Result.success(userService.removeBatchByIds(ids));
    }

    @Operation(summary = "查询用户")
    @PostMapping("findPageList")
    public Result<Page<UserBo>> findList(@RequestBody UserSearchReq userSearchReq) {
        return Result.success(userService.findPageList(userSearchReq));
    }

    private UserBo getUserBo(UserReq userReq) {
        final UserBo userBo = new UserBo();
        BeanUtils.copyProperties(userReq, userBo);
        return userBo;
    }
}
